package com.imooc.sell.controller;


import com.imooc.sell.Converter.OrderForm2OrderDTOConverter;
import com.imooc.sell.VO.ResultVo;
import com.imooc.sell.dataobject.OrderDetail;
import com.imooc.sell.dto.OrderDTO;
import com.imooc.sell.enums.ResultEnum;
import com.imooc.sell.exception.SellException;
import com.imooc.sell.form.OrderForm;
import com.imooc.sell.sevice.BuyerService;
import com.imooc.sell.sevice.OrderService;
import com.imooc.sell.utils.ResultVoUtil;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    //创建订单
    @PostMapping("/create")
    public ResultVo<Map<String,String>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            log.error("创建订单 参数不正确");
            throw new SellException(ResultEnum.Param_Error.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);

        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("购物车为空");
            throw new SellException(ResultEnum.Cart_Empty);
        }
        OrderDTO  createResult= orderService.create(orderDTO);

        Map<String,String> map = new HashMap<>();
        map.put("orderId",createResult.getOrderId());

        return ResultVoUtil.success(map);

    }

    //订单列表
    public ResultVo<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                            @RequestParam("page") Integer page,
                                            @RequestParam("size") Integer size){

        if(StringUtils.isEmpty(openid)){
            log.error("openid不能为空");
            throw  new SellException(ResultEnum.Param_Error);
        }
        PageRequest request = new PageRequest(page,size);
        //查询订单
        Page<OrderDTO> orderDTOPage = orderService.findList(openid,request);

        return  ResultVoUtil.success(orderDTOPage.getContent());
    }

    //订单详情
    @GetMapping("/detail")
    public ResultVo<OrderDTO> detail(@RequestParam("openid")String openid,
                                     @RequestParam("orderId")String orderId){

        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);

        return ResultVoUtil.success(orderDTO);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam("openid")String openid,
                           @RequestParam("orderId")String orderId){
        OrderDTO orderDTO = buyerService.cancelOrder(openid, orderId);

        return ResultVoUtil.success();

    }
}
